package com.tanhua.controller;

import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VisitorsVo;
import com.tanhua.server.CommentsService;
import com.tanhua.server.MovementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/movements")
@Slf4j
public class MovementController {
    @Autowired
    private MovementService movementService;
    @Autowired
    private CommentsService commentsService;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    /**
     * 发布动态
     * @param imageContent 动态Img
     * @param movement     动态文本内容
     * @return Void
     * @throws IOException
     */
    @PostMapping
    public ResponseEntity<Void> movements(MultipartFile[] imageContent, Movement movement) throws IOException {
        movementService.publishMovement(movement, imageContent);
        return ResponseEntity.ok(null);
    }

    /**
     * 我的动态
     *
     * @param page
     * @param pagesize
     * @return PageResult：<u>MovementVo</u>
     */
    @GetMapping("/all")
    public ResponseEntity findByUserId(Long userId,
                                       @RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = movementService.findByUserId(userId, page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 好友动态
     *
     * @param page
     * @param pagesize
     * @return PageResult：<u>MovementVo</u>
     */
    @GetMapping
    public ResponseEntity<PageResult> movements(@RequestParam(defaultValue = "1") Integer page,
                                                @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = movementService.findFriendMovements(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 推荐动态
     *
     * @param page
     * @param pagesize
     * @return PageResult：<u>MovementVo</u>
     */
    @GetMapping("/recommend")
    public ResponseEntity<PageResult> recommend(@RequestParam(defaultValue = "1") Integer page,
                                                @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = movementService.findRecommendMovements(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 根据UserId查询动态详情
     *
     * @param movementId
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<MovementsVo> findById(@PathVariable("id") String movementId) {
        MovementsVo movementsVo = movementService.findByMovementId(movementId);
        return ResponseEntity.ok(movementsVo);
    }

    /**
     * 互动喜欢
     * @param movementId 动态Id
     * @return countLike
     */
    @GetMapping("/{id}/like")
    public ResponseEntity<Integer> like(@PathVariable("id") String movementId) {
        Integer countLike = commentsService.countLike(movementId);
        log.info("countLike：" + countLike);
        return ResponseEntity.ok(countLike);
    }

    @GetMapping("/{id}/dislike")
    public ResponseEntity<Integer> dislike(@PathVariable("id") String movementId) {
        Integer countLike = commentsService.disLikeComment(movementId);
        log.info("countLike：" + countLike);
        return ResponseEntity.ok(countLike);
    }


    //love
    @GetMapping("/{id}/love")
    public ResponseEntity<Integer> love(@PathVariable("id") String movementId) {
        Integer countLike = commentsService.loveComment(movementId);
        log.info("loveLike：" + countLike);
        return ResponseEntity.ok(countLike);
    }

    @GetMapping("/{id}/unlove")
    public ResponseEntity<Integer> unlove(@PathVariable("id") String movementId) {
        Integer countLike = commentsService.disloveComment(movementId);
        log.info("loveLike：" + countLike);
        return ResponseEntity.ok(countLike);
    }

    /**
     * 谁看过我
     */
    @GetMapping("visitors")
    public ResponseEntity queryVisitorsList(){
        List<VisitorsVo> list = movementService.queryVisitorsList();
        return ResponseEntity.ok(list);
    }
}
